package com.beep.erp.model.home;


import android.support.design.widget.AppBarLayout;
import android.support.design.widget.CoordinatorLayout;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.TextView;

import com.beep.erp.R;
import com.beep.erp.common.base.BaseFragment;
import com.beep.erp.common.bean.BizGoodsListBean;
import com.beep.erp.common.bean.ClassifyBean;
import com.beep.erp.common.utils.MD5util;
import com.beep.erp.common.utils.TimeUtils;
import com.beep.erp.common.utils.ToastUtils;
import com.beep.erp.common.utils.ToolsUtils;
import com.beep.erp.common.widget.refresh.RefreshView;
import com.beep.erp.common.widget.refresh.container.DefaultHeader;
import com.beep.erp.http.HttpExceptionHandle;
import com.beep.erp.model.login.LoginActivity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;

/**
 * A simple {@link Fragment} subclass.
 */
public class HomeFragment extends BaseFragment<HomePresenter, HomeModel> implements HomeContract.View, HomeAdapter.OnItemClickLitener, RefreshView.OnFreshListener {

    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.appbar_layout)
    AppBarLayout appbarLayout;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.rl_instock)
    RecyclerView rlInstock;
    @BindView(R.id.super_refresh)
    RefreshView superRefresh;
    @BindView(R.id.activity_invite)
    CoordinatorLayout activityInvite;

    private ArrayList<ClassifyBean.ClassifyListBean> mBizGoodsList = new ArrayList<>();
    private HomeAdapter homeAdapter;
    private boolean isFirst = true;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_intstock_goods;
    }

    @Override
    protected void initView(View view) {
        tvTitle.setText(R.string.classify);
        mTipsHelper = createTipsHelper(superRefresh);
        superRefresh.setListener(this);
        superRefresh.setHeader(new DefaultHeader(getActivity(), true));
//        superRefresh.setFooter(new DefaultFooter(getActivity(), true));

        homeAdapter = new HomeAdapter(getActivity(), mBizGoodsList);
        homeAdapter.setOnItemClickLitener(this);
        rlInstock.setLayoutManager(new LinearLayoutManager(view.getContext()));
        rlInstock.setHasFixedSize(true);
        rlInstock.setNestedScrollingEnabled(false);
        rlInstock.setAdapter(homeAdapter);
    }

    @Override
    protected void lazyLoad() {
    }

    private void getClassify() {
        long mills = TimeUtils.getCurTimeMills();

        Map<String, String> map = new HashMap<>();
        map.put("type", "1");
        mPresenter.getGoodsClassify(MD5util.MD5Encode(ToolsUtils.buildSignStr(map, mills)), mills + "", "1");
    }

    @Override
    public void onItemClick(View view, int position) {
//        ClassifyActivity.start(getActivity(), mBizGoodsList.get(position).getValue());
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {
        superRefresh.onFinishFreshAndLoad();
        mTipsHelper.hideEmptyType();
        mTipsHelper.showLoading(false);
        mTipsHelper.hideLoading();
        mTipsHelper.hideError();
    }

    @Override
    public void showMessage(String message) {
        if (HttpExceptionHandle.ResponeThrowable.code == 9) {
            LoginActivity.start(getActivity());
            return;
        }
        if (ToolsUtils.isEmpty(message)) return;
        if (message.equals(getString(R.string.connection_timed_out)) || message.equals(getString(R.string.error)) || message.equals(getString(R.string.network_error)) || message.equals(getString(R.string.connection_failed)) || message.equals(getString(R.string.certificate_validation_failed))) {
            mTipsHelper.showError(true, message, v -> {
                mTipsHelper.hideError();
                getClassify();
            });
        } else {
            ToastUtils.showShort(message);
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        if (isFirst) {
            getData();
            isFirst = false;
        }
    }

    private void getData() {
        mTipsHelper.showLoading(true);
        mBizGoodsList.clear();
        homeAdapter.notifyDataSetChanged();
        getClassify();
    }

    @Override
    public void getDate() {
    }

    @Override
    public void getBizGoods(BizGoodsListBean bizGoodsListBean) {
    }

    @Override
    public void getGoodsClassify(ClassifyBean classifyBean) {
        if (ToolsUtils.isEmptyList(classifyBean.getClassifyList())) {
            ToastUtils.showShort(getString(R.string.no_data));
            return;
        }
        mBizGoodsList.addAll(classifyBean.getClassifyList());
        homeAdapter.notifyDataSetChanged();
        if (classifyBean.getClassifyList().size() == 0) {
            mTipsHelper.showEmptyType(getString(R.string.emptymessage15));
        }
    }

    @Override
    public void onRefresh() {
        getData();
    }

    @Override
    public void onLoadmore() {

    }
}
